package com.controller.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.comm.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class AlipayController {

/*    @Autowired
    private OrderService orderService;*/

    @RequestMapping(value = "/alipay" ,produces = {"text/html;charset=UTF-8"})
    public void alipay(String order_id, String pay_total_price, String product_name, HttpServletResponse response) throws AlipayApiException {
        String URL = "https://openapi.alipaydev.com/gateway.do"; // alipay测试支付地址
        // 生产环境支付地址为https://openapi.alipay.com/gateway.do
        String APP_ID = "2021000122609377";   // 创建的应用ID
        String APP_PRIVATE_KEY = Constants.APP_PRIVATE_KEY; //自己的私钥
        String ALIPAY_PUBLIC_KEY = Constants.ALIPAY_PUBLIC_KEY; //支付宝的公钥
        //下载对应SDK，jar依赖
        AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY,"json", "UTF-8", ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest(); //  创建API对应的request
//        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();    // 创建API对应的request
        alipayRequest.setReturnUrl("http://localhost:8080/alipaySuccess");        // 用户确认支付后，支付宝get请求returnUrl
        alipayRequest.setNotifyUrl("http://localhost:8080/payReturnServlet.do");    // 交易成功后，支付宝post请求notifyUrl（商户入参传入）
        /*
            由于同步返回的不可靠性，支付结果必须以异步通知或查询接口返回为准，不能依赖同步跳转。
            商户系统接收到异步通知以后，必须通过验签（验证通知中的sign参数）来确保支付通知是由支付宝发送的。
         */
        /*
             填充业务参数
                 out_trade_no   每笔订单号需要自身的唯一id，支付宝会根据此id判断是否交易
                 total_amount   这笔订单的价格
                 subject        订单商品名称
                 product_code   支付方式：QUICK_WAP_PAY 登录账号支付
             这四个参数应该从结算页面传递过来
         */
        String out_trade_no = order_id;    // 每笔订单号需要自身的唯一id，支付宝会根据此id判断是否交易
        String total_amount = pay_total_price;                 // 这笔订单的价格
        String subject = product_name;               // 订单商品名称
//       String product_code = "FAST_INSTANT_TRADE_PAY";        // 支付方式：QUICK_WAP_PAY 登录账号支付
//        String product_code = "QUICK_WAP_PAY";        // 支付方式：QUICK_WAP_PAY 登录账号支付
        String product_code="FAST_INSTANT_TRADE_PAY";
        String qr_pay_mode="4";
        String bizContent = "{" +
                "\"out_trade_no\":\"" + out_trade_no + "\"," +
                "\"total_amount\":" + total_amount + "," +
                "\"subject\":\"" + subject + "\"," +
                "\"product_code\":\"" + product_code + ","+
                "\"product_code\":\""+qr_pay_mode
                        +"}";          // 生成支付json数据
        System.out.println("支付生成json数据 bizContent=" + bizContent);

        alipayRequest.setBizContent(bizContent);                         // 提交支付请求
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(form);//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

/*    @RequestMapping(value = "/alipaySuccess",method = RequestMethod.GET)
    public String alipaySuccess(String out_trade_no,String timestamp){
        orderService.alipaySuccess(out_trade_no,timestamp);
        return "/success.html";
    }*/
}
